﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace QL_DonDatHang
{
    public class Program_DDH
    {
        
        /// <summary>
        /// ///////////////
        /// </summary>
        #region ThuocTinh
        private string linkserver=@"Server=.\SQLEXPRESS; Database=QL_BANHANG_GIOCHA; Integrated Security=True";
        private SqlConnection dataBase;
        
        public SqlConnection DataBase
        {
            get
            {
                return dataBase;
            }

            set
            {
                dataBase = value;
            }
        }


        public int IdDonHang
        {
            get
            {
                return idDonHang;
            }

            set
            {
                idDonHang = value;
            }
        }

        public DataTable DtThanhToan
        {
            get
            {
                return dtThanhToan;
            }

            set
            {
                dtThanhToan = value;
            }
        }

        private DataTable dtThanhToan;
        public int idDonHang;
        #endregion

        /// <summary>
        /// //////////////////////////////
        /// </summary>
        #region KhoiTao
        public Program_DDH()
        {
            DataBase = new SqlConnection(linkserver);
            DataBase.Open();
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        #region PhuongThuc
        public string select_ThanhToan()
        {
            string sqlthanhtoan = "SELECT(QL_MATHANG.DONGIA + QL_DONDATHANG.SOLUONG)AS THANHTIEN FROM QL_MATHANG, QL_DONDATHANG WHERE QL_DONDATHANG.TEN_MATHANG = QL_MATHANG.TEN_MATHANG AND QL_DONDATHANG.ID_DONHANG = "+IdDonHang;
            SqlDataAdapter da = new SqlDataAdapter(sqlthanhtoan, DataBase);
            DtThanhToan = new DataTable();
            da.Fill(DtThanhToan);
            if (DtThanhToan.Rows.Count == 0)
                return "---";
            return DtThanhToan.Rows[0][0].ToString();

        }
        public DataTable slect_ThanhToan(int id)
        {
            
            string sqlThanhToan = "SELECT QL_DONDATHANG.TEN_KHACHHANG,QL_MATHANG.TEN_MATHANG,QL_DONDATHANG.SOLUONG,QL_MATHANG.DONGIA,(QL_DONDATHANG.SOLUONG * QL_MATHANG.DONGIA) AS THANHTIEN FROM QL_DONDATHANG, QL_MATHANG WHERE QL_DONDATHANG.TEN_MATHANG = QL_MATHANG.TEN_MATHANG AND QL_DONDATHANG.ID_DONHANG = "+id;
            SqlDataAdapter sda = new SqlDataAdapter(sqlThanhToan,DataBase);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
            
        }
        public bool XoaDonHang(int id)
        {
            SqlCommand cmd = new SqlCommand("XoaDonHang", DataBase);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", id);
            int count = cmd.ExecuteNonQuery();
            if (count > 0)
            {
                return true;
            }
            else
                return false;
        }
        #endregion
    }
}
